﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Infrastructures
{
    public class RequestHelper
    {
        public static string Send(string url, string data = "")
        {
            HttpWebResponse response = (HttpWebResponse)GetRequest(url, data).GetResponse();
            string strValue = "";
            using (StreamReader Reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                strValue = Reader.ReadToEnd();
            }

            return strValue;
        }

        private static HttpWebRequest GetRequest(string url, string data = "")
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/json;charset=utf-8";
            request.Proxy = new WebProxy("10.166.17.101", 8080);
            request.KeepAlive = false;

            if (!data.IsNullOrEmpty())
            {
                UTF8Encoding encoding = new UTF8Encoding();
                byte[] values = encoding.GetBytes(data);
                request.ContentLength = values.Length;

                Stream sm = request.GetRequestStream();
                sm.Write(values, 0, values.Length);
                sm.Close();
            }

            return request;
        }
    }
}
